package com.jt.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ContentCatMapper;
import com.jt.pojo.ContentCat;
import com.jt.service.ContentCatService;
import com.jt.utils.Assert;
import com.jt.vo.EasyUITree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author dc
 * @Classname ContentCatServiceImpl
 * @Date 2020/3/23 17:36
 */
@Service
public class ContentCatServiceImpl implements ContentCatService {

    @Autowired
    private ContentCatMapper contentCatMapper;

    @Override
    public List<EasyUITree> getContentCatByParentId(Long parentId) {
        //参数校验
        Assert.isValid(parentId<0,"父节点id错误");
        QueryWrapper<ContentCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<ContentCat> catList = contentCatMapper.selectList(queryWrapper);
        List<EasyUITree> rs= new ArrayList<>();
        for(ContentCat cat:catList){
            EasyUITree node =new EasyUITree();
            node.setId(cat.getId());
            node.setText(cat.getName());
            node.setState(cat.getIsParent()?"closed":"open");
            rs.add(node);
        }
        return rs;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ContentCat createNode(Long parentId,String name) {
        Assert.isValid(parentId<0||name==null,"父节点信息有误!");
        //添加新节点
        ContentCat rs = new ContentCat();
        rs.setParentId(parentId).setName(name)
                .setIsParent(false).setStatus(1)
                .setSortOrder(1).setCreated(new Date())
                .setUpdated(rs.getCreated());
        int rows = contentCatMapper.insert(rs);
        Assert.isServiceValid(rows<0,"添加新内容节点失败");
        //查看父节点isParent是否为true
        ContentCat parent = contentCatMapper.selectById(parentId);
        if(!parent.getIsParent()){
            parent.setIsParent(true).setUpdated(rs.getCreated());
            //更新父节点
            int parentRow = contentCatMapper.updateById(parent);
            Assert.isServiceValid(parentRow<0,"更新内容父节点信息失败");
        }
        return rs;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void deleteNode(Long id) {
        //参数校验
        Assert.isValid(id == null, "要删除的节点可能不存在");
        //判定此节点下是否有子节点
        QueryWrapper<ContentCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        Integer rows = contentCatMapper.selectCount(queryWrapper);
        Assert.isServiceValid(rows > 0, "请先删除子节点!");
        //获取此节点的父节点id
        ContentCat target = contentCatMapper.selectById(id);
        Long parentId = target.getParentId();
        //删除此节点
        int delete = contentCatMapper.deleteById(id);
        Assert.isServiceValid(delete == 0, "删除节点失败!");
        //判断此节点的父节点是否还有节点,若没有则修改isParent属性值
        QueryWrapper<ContentCat> query = new QueryWrapper<>();
        query.eq("parent_id", parentId);
        Integer count = contentCatMapper.selectCount(query);
        if (count == 0) {
            ContentCat contentCat = new ContentCat();
            contentCat.setId(parentId).setIsParent(false).setUpdated(new Date());
            contentCatMapper.updateById(contentCat);
        }
    }

    @Override
    public void updateNodeById(Long id, String name) {
        //参数校验
        Assert.isValid(id<0,"请先选择要修改名称的节点!");
        Assert.isValid(StringUtils.isEmpty(name),"节点名不能为空");
        //修改节点信息
        ContentCat entity = new ContentCat();
        entity.setId(id).setName(name).setUpdated(new Date());
        int row = contentCatMapper.updateById(entity);
        Assert.isServiceValid(row==0,"网络繁忙,请稍微再试");
    }
}
